# vim: ts=4:sw=4:syntax=perl

use gri_secmgr qw(get_user_name);
use gri_portal_config;

sub mm_primary_site_role_process {

	# Give a warning if the role count is not 0.
	my $info=$::GRI_FRONTEND->get_role_count();
	if(defined($info->{error})) {
		return html_error("Error whilst reading role information:<br>$info->{error}",0);
	}
	my $username=get_user_name();
	if(!defined($username) || $username eq "public") {
		return "This page is not available unless properly logged in.";
	}
	if($info->{count}) {
		return "Someone is already defined with admin access for this site.";
	}
	# Now all the checks have been done make this person a member of all roles!!

	my ($roles,$err)=$::GRI_FRONTEND->get_defined_roles();
	if(!defined($roles)) {
		return html_error("Failed to load default role settings:<br>$err",0);
	}
	my @R=(keys(%$roles));
	my ($rc2,$err2)=$::GRI_FRONTEND->write_user_role($username,[@R]);
	if($rc2==0) {
		return html_error("Failed to set initial role:<br>$err2",0);
	}
	# Ok - Indicate role has been added and allow user to return to main page.
	my $l=link_to_str("stdlink","Front Page","_","all_portals=0,all_sections=0");
	my $cc="Congratulations - user '$username' is the first user account with all administration roles for this site. Please use carefully. Now you can return to the $l.";
	return $cc;
}

1;
